package accs.gui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.nio.charset.Charset;
import java.text.MessageFormat;
import java.util.regex.Pattern;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SpinnerNumberModel;

import net.miginfocom.swing.MigLayout;
import accs.gui.logic.AccountCheckerTask;
import accs.gui.logic.CheckerTask;
import accs.model.AccountChecker;
import accs.model.io.DuplicateFilter;
import accs.model.io.LineUserPasswordReader;
import accs.model.io.MailUserFilter;
import accs.model.io.ProxyReader;
import accs.model.io.RegexUserPasswordReader;
import accs.model.io.SeparatedUserPasswordReader;
import accs.model.io.SimpleUserPasswordReader;
import accs.model.io.TrimFilter;
import accs.model.io.UserPasswordReader;

public class AccountCheckerDialog extends JDialog {
	
	private static final Charset[] CHARSETS=Charset.availableCharsets().values().toArray(new Charset[0]);
	private static final Charset DEFAULT_CHARSET=Charset.forName("utf-8");
	
	public static enum INPUT_FORMAT_TYPE{SIMPLE, LINE, SEPARATED, REGEXP;}
	
	private JTextField inputField;
	private JPanel inputFormatPanel;
	private JPanel simplePanel;
	private JPanel separatedPanel;
	private JTextField separatedUserPrefix;
	private JTextField separatedPassPrefix;
	private JTextField outputField;
	private JTextField simpleDelim;
	private JTextField simplePrefix;
	private JSpinner threadSpinner;
	private JComboBox<INPUT_FORMAT_TYPE> inputFormatChooser;

	private AccountChecker checker;
	private AccountCheckerTask task=null;
	private JCheckBox mailCutCheckBox;
	private JCheckBox trimUserPassCheckBox;
	private JTextField lineDelim;
	private JSpinner lineUserCol;
	private JCheckBox duplicatesCheckBox;
	private JComboBox inputEncodingBox;
	private JComboBox outputEncodingBox;
	private JToggleButton newLineWinButton;
	private JToggleButton newLineUnixButton;
	private JToggleButton newLineMacButton;
	private JCheckBox useProxiesCheckBox;
	private JTextField proxyFileField;
	private JButton proxyFileButton;
	private JSpinner linePassCol;
	private JTextField regexpPattern;
	private JSpinner regexpUserGroup;
	private JSpinner regexpPasswordGroup;
	
	private static final String DIALOG_TITLE="Create AccountCheckerTask: {0}";
	
	public AccountCheckerDialog(JFrame owner) {
		super(owner,DIALOG_TITLE,true);
		setSize(445,300);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		
		addComponentListener(new ComponentAdapter() {
			public void componentShown(ComponentEvent arg0) {
				task=null;
			}
		});
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		getContentPane().add(tabbedPane, BorderLayout.CENTER);
		
		JPanel inputPanel = new JPanel();
		tabbedPane.addTab("Input", null, inputPanel, null);
		inputPanel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_2 = new JPanel();
		inputPanel.add(panel_2, BorderLayout.NORTH);
		panel_2.setLayout(new MigLayout("", "[28px][grow][]", "[23px][][23px]"));
		
		JButton chooseInputButton = new JButton("...");
		chooseInputButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser=new JFileChooser();
				chooser.setSelectedFile(new File(inputField.getText()));
				chooser.showOpenDialog(AccountCheckerDialog.this);
				
				inputField.setText(chooser.getSelectedFile().getAbsolutePath());
			}
		});
		
		inputField = new JTextField();
		panel_2.add(inputField, "flowy,cell 1 0,grow");
		inputField.setColumns(10);
		panel_2.add(chooseInputButton, "cell 2 0,alignx left,aligny top");
		
		JLabel lblNewLabel_2 = new JLabel("File:");
		panel_2.add(lblNewLabel_2, "cell 0 0,alignx right");
		
		inputFormatChooser = new JComboBox<INPUT_FORMAT_TYPE>();
		inputFormatChooser.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent evt) {
				CardLayout lyt=(CardLayout)inputFormatPanel.getLayout();
				lyt.show(inputFormatPanel,evt.getItem().toString());
			}
		});
		
		JLabel lblEncoding = new JLabel("Encoding:");
		panel_2.add(lblEncoding, "cell 0 1,alignx trailing");
		
		inputEncodingBox = new JComboBox(CHARSETS);
		inputEncodingBox.setSelectedItem(DEFAULT_CHARSET);
		panel_2.add(inputEncodingBox, "flowx,cell 1 1 2 1,growx");
		
		JLabel lblNewLabel_1 = new JLabel("Format:");
		panel_2.add(lblNewLabel_1, "cell 0 2,alignx right");
		inputFormatChooser.setModel(new DefaultComboBoxModel<INPUT_FORMAT_TYPE>(INPUT_FORMAT_TYPE.values()));
		panel_2.add(inputFormatChooser, "cell 1 2,alignx left,growy");
		
		JButton btnUtf = new JButton("UTF");
		btnUtf.setToolTipText("UTF-8");
		btnUtf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				inputEncodingBox.setSelectedItem(Charset.forName("utf-8"));
			}
		});
		panel_2.add(btnUtf, "cell 1 1 2 1");
		
		JButton btnNewButton_2 = new JButton("ISO");
		btnNewButton_2.setToolTipText("ISO-8859-1");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				inputEncodingBox.setSelectedItem(Charset.forName("iso-8859-1"));
			}
		});
		panel_2.add(btnNewButton_2, "cell 1 1 2 1");
		
		JButton btnDef = new JButton("DEF");
		btnDef.setToolTipText("Default charset: "+Charset.defaultCharset().toString());
		btnDef.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				inputEncodingBox.setSelectedItem(Charset.defaultCharset());
			}
		});
		panel_2.add(btnDef, "cell 1 1");
		
		inputFormatPanel = new JPanel();
		inputPanel.add(inputFormatPanel, BorderLayout.CENTER);
		inputFormatPanel.setLayout(new CardLayout(0, 0));
		
		simplePanel = new JPanel();
		inputFormatPanel.add(simplePanel,INPUT_FORMAT_TYPE.SIMPLE.toString());
		simplePanel.setLayout(new MigLayout("", "[][grow]", "[][][]"));
		
		JLabel lblExample = new JLabel("Example:");
		simplePanel.add(lblExample, "cell 0 0");
		
		JLabel lblLoginUserpass = new JLabel("Login user:pass (Delimiter :, Prefix Login )");
		simplePanel.add(lblLoginUserpass, "cell 1 0");
		
		JLabel lblNewLabel = new JLabel("Delimiter:");
		simplePanel.add(lblNewLabel, "cell 0 1,alignx trailing");
		
		simpleDelim = new JTextField();
		simpleDelim.setText(":");
		simplePanel.add(simpleDelim, "cell 1 1,growx");
		simpleDelim.setColumns(10);
		
		JLabel lblPrefix = new JLabel("Prefix:");
		simplePanel.add(lblPrefix, "cell 0 2,alignx trailing");
		
		simplePrefix = new JTextField();
		simplePanel.add(simplePrefix, "cell 1 2,growx");
		simplePrefix.setColumns(10);
		
		separatedPanel = new JPanel();
		inputFormatPanel.add(separatedPanel,INPUT_FORMAT_TYPE.SEPARATED.toString());
		separatedPanel.setLayout(new MigLayout("", "[][grow]", "[][][]"));
		
		JLabel lblExample_1 = new JLabel("Example:");
		separatedPanel.add(lblExample_1, "cell 0 0,alignx right");
		
		JLabel lblUserUser = new JLabel("<html>User: user<br>Password: password</html>");
		separatedPanel.add(lblUserUser, "cell 1 0");
		
		JLabel lblUsernameprefix = new JLabel("Username-Prefix:");
		separatedPanel.add(lblUsernameprefix, "cell 0 1,alignx right");
		
		separatedUserPrefix = new JTextField();
		separatedUserPrefix.setText("Username:");
		separatedPanel.add(separatedUserPrefix, "cell 1 1,growx");
		separatedUserPrefix.setColumns(10);
		
		JLabel lblPasswordprefix = new JLabel("Password-Prefix:");
		separatedPanel.add(lblPasswordprefix, "cell 0 2,alignx right");
		
		separatedPassPrefix = new JTextField();
		separatedPassPrefix.setText("Password:");
		separatedPanel.add(separatedPassPrefix, "cell 1 2,growx");
		separatedPassPrefix.setColumns(10);
		
		JPanel linePanel = new JPanel();
		inputFormatPanel.add(linePanel, INPUT_FORMAT_TYPE.LINE.toString());
		linePanel.setLayout(new MigLayout("", "[][grow]", "[][][][]"));
		
		JLabel lblExample_2 = new JLabel("Example:");
		linePanel.add(lblExample_2, "cell 0 0,alignx right");
		
		JLabel lblUserPassword = new JLabel("user | password | foo | bar (delim: |, user-col: 1, password-col: 2)");
		linePanel.add(lblUserPassword, "cell 1 0");
		
		JLabel lblDelim = new JLabel("Delimiter:");
		linePanel.add(lblDelim, "cell 0 1,alignx right");
		
		lineDelim = new JTextField();
		lineDelim.setText("|");
		linePanel.add(lineDelim, "cell 1 1,growx");
		lineDelim.setColumns(10);
		
		JLabel lblUsercolumn = new JLabel("Username-Column:");
		linePanel.add(lblUsercolumn, "cell 0 2,alignx right");
		
		lineUserCol = new JSpinner();
		lineUserCol.setModel(new SpinnerNumberModel(1, 1, 100, 1));
		linePanel.add(lineUserCol, "cell 1 2,growx");
		
		JLabel lblPasswordcolumn = new JLabel("Password-Column:");
		linePanel.add(lblPasswordcolumn, "cell 0 3,alignx right");
		
		linePassCol = new JSpinner();
		linePassCol.setModel(new SpinnerNumberModel(2, 1, 100, 1));
		linePanel.add(linePassCol, "cell 1 3,growx");
		
		JPanel regexpPanel = new JPanel();
		inputFormatPanel.add(regexpPanel, INPUT_FORMAT_TYPE.REGEXP.toString());
		regexpPanel.setLayout(new MigLayout("", "[][grow]", "[][][][]"));
		
		JLabel lblInfo_1 = new JLabel("Info:");
		regexpPanel.add(lblInfo_1, "cell 0 0,alignx right");
		
		JLabel lblPleaseInformYourself = new JLabel("Please inform yourself about regular expressions.");
		regexpPanel.add(lblPleaseInformYourself, "cell 1 0");
		
		JLabel lblPattern = new JLabel("Pattern:");
		regexpPanel.add(lblPattern, "cell 0 1,alignx trailing");
		
		regexpPattern = new JTextField();
		regexpPattern.setText("([^:]*):([^:|]*)\\s*|.*");
		regexpPanel.add(regexpPattern, "cell 1 1,growx");
		regexpPattern.setColumns(10);
		
		JLabel lblUsergroup = new JLabel("User-Group:");
		regexpPanel.add(lblUsergroup, "cell 0 2,alignx right");
		
		regexpUserGroup = new JSpinner();
		regexpUserGroup.setModel(new SpinnerNumberModel(1, 1, 50, 1));
		regexpPanel.add(regexpUserGroup, "cell 1 2,growx");
		
		JLabel lblPasswordgroup = new JLabel("Password-Group:");
		regexpPanel.add(lblPasswordgroup, "cell 0 3,alignx right");
		
		regexpPasswordGroup = new JSpinner();
		regexpPasswordGroup.setModel(new SpinnerNumberModel(2, 1, 50, 1));
		regexpPanel.add(regexpPasswordGroup, "cell 1 3,growx");
		
		JPanel outputPanel = new JPanel();
		tabbedPane.addTab("Output", null, outputPanel, null);
		outputPanel.setLayout(new MigLayout("", "[][grow][]", "[][][]"));
		
		JLabel lblNewLabel_3 = new JLabel("File:");
		outputPanel.add(lblNewLabel_3, "cell 0 0,alignx right");
		
		outputField = new JTextField();
		outputPanel.add(outputField, "cell 1 0,growx");
		outputField.setColumns(10);
		
		JButton outputChooserButton = new JButton("...");
		outputChooserButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser=new JFileChooser();
				chooser.setSelectedFile(new File(outputField.getText()));
				chooser.showSaveDialog(AccountCheckerDialog.this);
				
				outputField.setText(chooser.getSelectedFile().getAbsolutePath());
			}
		});
		outputPanel.add(outputChooserButton, "flowx,cell 2 0");
		
		JLabel lblEncoding_1 = new JLabel("Encoding:");
		outputPanel.add(lblEncoding_1, "cell 0 1,alignx right");
		
		outputEncodingBox = new JComboBox(CHARSETS);
		outputEncodingBox.setSelectedItem(DEFAULT_CHARSET);
		outputPanel.add(outputEncodingBox, "flowx,cell 1 1 2 1,growx");
		
		JButton btnUtf_1 = new JButton("UTF");
		btnUtf_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				outputEncodingBox.setSelectedItem(Charset.forName("utf-8"));
			}
		});
		btnUtf_1.setToolTipText("UTF-8");
		outputPanel.add(btnUtf_1, "cell 1 1 2 1");
		
		JButton btnNewButton_4 = new JButton("ISO");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				outputEncodingBox.setSelectedItem(Charset.forName("iso-8859-1"));
			}
		});
		btnNewButton_4.setToolTipText("ISO-8859-1");
		outputPanel.add(btnNewButton_4, "cell 1 1 2 1");
		
		JButton btnNewButton_3 = new JButton("DEF");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				outputEncodingBox.setSelectedItem(Charset.defaultCharset());
			}
		});
		btnNewButton_3.setToolTipText("Default charset: "+Charset.defaultCharset().toString());
		outputPanel.add(btnNewButton_3, "cell 1 1 2 1");
		
		JLabel lblNewLine = new JLabel("New Line:");
		outputPanel.add(lblNewLine, "cell 0 2,alignx right");
		
		ButtonGroup group=new ButtonGroup();
		String newLine=System.getProperty("line.separator");
		
		newLineWinButton = new JToggleButton("Win");
		newLineWinButton.setToolTipText("CR+LF (\\r\\n)");
		newLineWinButton.setSelected("\r\n".equals(newLine));
		group.add(newLineWinButton);
		outputPanel.add(newLineWinButton, "flowx,cell 1 2");
		
		newLineUnixButton = new JToggleButton("Unix");
		newLineUnixButton.setSelected("\n".equals(newLine));
		newLineUnixButton.setToolTipText("LF (\\n)");
		group.add(newLineUnixButton);
		outputPanel.add(newLineUnixButton, "cell 1 2");
		
		newLineMacButton = new JToggleButton("Mac");
		newLineMacButton.setSelected("\r".equals(newLine));
		newLineMacButton.setToolTipText("CR (\\r)");
		group.add(newLineMacButton);
		outputPanel.add(newLineMacButton, "cell 1 2");
		
		JButton btnNewButton_5 = new JButton("auto");
		btnNewButton_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String totalPath=inputField.getText();
				
				int totalPathIndex=totalPath.lastIndexOf(File.separatorChar);
				String path="";
				String name=totalPath;
				if(totalPathIndex!=-1) {
					path=totalPath.substring(0,totalPathIndex);
					name=totalPath.substring(totalPathIndex+1);
				}
				
				if(name.contains(".")) {
					int index=name.lastIndexOf(".");
					String prefix=name.substring(0,index);
					String suffix=name.substring(index+1);
					name=prefix+"_checked"+"."+suffix;
				} else {
					name+="_checked";
				}
				
				outputField.setText(path+File.separator+name);
			}
		});
		btnNewButton_5.setToolTipText("Generates the path automatically out of the input file path.");
		outputPanel.add(btnNewButton_5, "cell 2 0");
		
		JPanel extraPanel = new JPanel();
		tabbedPane.addTab("Extras", null, extraPanel, null);
		extraPanel.setLayout(new MigLayout("", "[][grow]", "[][][][][]"));
		
		JLabel lblThreads = new JLabel("Threads:");
		extraPanel.add(lblThreads, "cell 0 0,alignx left");
		
		threadSpinner = new JSpinner();
		threadSpinner.setModel(new SpinnerNumberModel(1, 1, 100, 1));
		extraPanel.add(threadSpinner, "cell 1 0,alignx left");
		
		JLabel lblMailcheck = new JLabel("Mail-Check:");
		extraPanel.add(lblMailcheck, "cell 0 1,alignx left");
		
		JCheckBox chckbxNewCheckBox = new JCheckBox("Check Mails and save the result next to the other");
		chckbxNewCheckBox.setEnabled(false);
		extraPanel.add(chckbxNewCheckBox, "cell 1 1,growx");
		
		JLabel lblDuplicates = new JLabel("Duplicates:");
		extraPanel.add(lblDuplicates, "cell 0 2");
		
		duplicatesCheckBox = new JCheckBox("Remove Duplicates");
		duplicatesCheckBox.setSelected(true);
		extraPanel.add(duplicatesCheckBox, "cell 1 2");
		
		JLabel lblMailcut = new JLabel("Mail-Cut:");
		extraPanel.add(lblMailcut, "cell 0 3,alignx left");
		
		mailCutCheckBox = new JCheckBox("Cut the username out of the mail address (foo@bar.com -> foo)");
		extraPanel.add(mailCutCheckBox, "cell 1 3");
		
		JLabel lblTrim = new JLabel("Trim:");
		extraPanel.add(lblTrim, "cell 0 4,alignx left");
		
		trimUserPassCheckBox = new JCheckBox("Remove all leading+trailing whitespaces of user+password");
		trimUserPassCheckBox.setSelected(true);
		extraPanel.add(trimUserPassCheckBox, "cell 1 4");
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Proxy", null, panel_1, null);
		panel_1.setLayout(new MigLayout("", "[][grow]", "[][][][]"));
		
		JLabel lblInfo = new JLabel("Info:");
		panel_1.add(lblInfo, "cell 0 0,alignx right");
		
		JLabel lblThisFeatureIs = new JLabel("This feature does not work for formatter and mail checker.");
		panel_1.add(lblThisFeatureIs, "cell 1 0");
		
		JLabel lblFormat = new JLabel("Format:");
		panel_1.add(lblFormat, "cell 0 1,alignx right");
		
		JLabel lblHostport = new JLabel("host:port");
		panel_1.add(lblHostport, "cell 1 1");
		
		JLabel lblUseProxies = new JLabel("Use Proxies:");
		panel_1.add(lblUseProxies, "cell 0 2,alignx right");
		
		useProxiesCheckBox = new JCheckBox("Use HTTP/HTTPS-Proxies for all connections");
		useProxiesCheckBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				proxyFileField.setEnabled(useProxiesCheckBox.isSelected());
				proxyFileButton.setEnabled(useProxiesCheckBox.isSelected());
			}
		});
		panel_1.add(useProxiesCheckBox, "cell 1 2");
		
		JLabel lblProxyfile = new JLabel("Proxy-File:");
		panel_1.add(lblProxyfile, "cell 0 3,alignx right");
		
		proxyFileField = new JTextField();
		proxyFileField.setEnabled(false);
		panel_1.add(proxyFileField, "flowx,cell 1 3,growx");
		proxyFileField.setColumns(10);
		
		proxyFileButton = new JButton("...");
		proxyFileButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser=new JFileChooser();
				chooser.setSelectedFile(new File(proxyFileField.getText()));
				chooser.showSaveDialog(AccountCheckerDialog.this);
				
				proxyFileField.setText(chooser.getSelectedFile().getAbsolutePath());
			}
		});
		proxyFileButton.setEnabled(false);
		panel_1.add(proxyFileButton, "cell 1 3");
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		
		JButton btnNewButton = new JButton("Start");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				try {
					if(createTask()) {
						setVisible(false);
					}
				} catch(Exception e) {
					JOptionPane.showMessageDialog(AccountCheckerDialog.this,e.getMessage(),"Error: "+e.getClass().getName(),JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Cancel");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}
		});
		panel.add(btnNewButton_1);
		
		pack();
	}
	
	public void setAccountChecker(AccountChecker checker) {
		this.checker=checker;
		this.setTitle(MessageFormat.format(DIALOG_TITLE,checker.getName()));
	}
	
	public CheckerTask getTask() {
		return task;
	}
	
	private boolean createTask() throws FileNotFoundException, IOException {
		File inputFile=new File(inputField.getText());
		File outputFile=new File(outputField.getText());
		
		boolean append=false;
		
		if(!inputFile.exists() || !inputFile.isFile()) throw new IllegalArgumentException("Input File is not existing or not a file!");
		if(outputFile.exists()) {
			//int res=JOptionPane.showConfirmDialog(this,"The output file is existing.\nPress 'Yes' for overwrite.\nPress 'No' for appending.\nPress 'Cancel' for cancel","Output file existing.",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
			int res=JOptionPane.showOptionDialog(this,"The output file is existing.\nChoose one of the following actions.","Output file existing",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,new Object[]{"Overwrite","Append","Cancel"},"Overwrite");
			//JOptionPane.show
			if(res==JOptionPane.YES_OPTION) { System.out.println("yes");
				append=false;
			} else if(res==JOptionPane.NO_OPTION) { System.out.println("no");
				append=true;
			} else { System.out.println("cancel");
				return false;
			}
		}
		
		task=new AccountCheckerTask("AccountChecker: "+checker.getName()+" ("+inputFile.getName()+" -> "+outputFile.getName()+")");
		
		Reader fileReader=new InputStreamReader(new FileInputStream(inputFile),(Charset)inputEncodingBox.getSelectedItem());
		UserPasswordReader reader=null;
		
		if(inputFormatChooser.getSelectedItem().equals(INPUT_FORMAT_TYPE.SIMPLE)) {
			reader=new SimpleUserPasswordReader(fileReader,simpleDelim.getText(),simplePrefix.getText());
		} else if(inputFormatChooser.getSelectedItem().equals(INPUT_FORMAT_TYPE.SEPARATED)) {
			reader=new SeparatedUserPasswordReader(fileReader,separatedUserPrefix.getText(),separatedPassPrefix.getText());
		} else if(inputFormatChooser.getSelectedItem().equals(INPUT_FORMAT_TYPE.LINE)) {
			reader=new LineUserPasswordReader(fileReader,lineDelim.getText(),Integer.parseInt(lineUserCol.getValue().toString())-1,Integer.parseInt(linePassCol.getValue().toString())-1);
		} else if(inputFormatChooser.getSelectedItem().equals(INPUT_FORMAT_TYPE.REGEXP)) {
			reader=new RegexUserPasswordReader(fileReader, Pattern.compile(regexpPattern.getText()), Integer.parseInt(regexpUserGroup.getValue().toString()), Integer.parseInt(regexpPasswordGroup.getValue().toString()));
		}
		
		if(duplicatesCheckBox.isSelected()) reader=new DuplicateFilter(reader);
		if(mailCutCheckBox.isSelected()) reader=new MailUserFilter(reader);
		if(trimUserPassCheckBox.isSelected()) reader=new TrimFilter(reader);
		
		task.loadUserPasswordPairs(reader);
		
		if(newLineWinButton.isSelected()) {
			task.setNewLine("\r\n");
		} else if(newLineUnixButton.isSelected()) {
			task.setNewLine("\n");
		} else if(newLineMacButton.isSelected()) {
			task.setNewLine("\r");
		}
		
		if(useProxiesCheckBox.isSelected()) {
			ProxyReader proxReader=new ProxyReader(new SimpleUserPasswordReader(new FileReader(new File(proxyFileField.getText()))));
			task.setProxyList(proxReader.readAll());
		}
		
		task.setOutput(new OutputStreamWriter(new FileOutputStream(outputFile,append),(Charset)outputEncodingBox.getSelectedItem()));
		task.setNumberOfThreads((Integer)threadSpinner.getValue());
		task.setAccountChecker(checker);
		
		return true;
	}
}